﻿/***
 * 
 * 系统设置
 * 
 */


namespace SG
{

    public class GameSettingManager
    {
        private const string SaveName = "GameSetting.bytes";

        #region 静态接口
        private static GameSettingManager m_instance;
        public static GameSettingManager Instance()
        {
            if (m_instance == null)
            {
                m_instance = new GameSettingManager();
                m_instance.DoInitData();
            }
            return m_instance;
        }
        /// <summary>
        /// 读取缓存文件
        /// </summary>
        //public static GameSettingData LoadCache()
        //{
        //    string filePath = MotionFramework.Resource.AssetPathHelper.MakePersistentLoadPath(SaveName);
        //    string jsonData = FileUtility.ReadFile(filePath);
        //    if (jsonData == string.Empty)
        //        return null;
        //    return JsonUtility.FromJson<GameSettingData>(jsonData);
        //}
        ///// <summary>
        ///// 保存缓存文件
        ///// </summary>
        //public static void SaveCache(GameSettingData settingData)
        //{
        //    string filePath = MotionFramework.Resource.AssetPathHelper.MakePersistentLoadPath(SaveName);
        //    string jsonData = JsonUtility.ToJson(settingData);
        //    FileUtility.CreateFile(filePath, jsonData);
        //}
        #endregion

        GameSettingData mSettingData;

        ModifyBase mModify = new ModifyBase();

        void DoInitData()
        {
            //mSettingData = LoadCache();
            if (mSettingData == null)
            {
                mSettingData = new GameSettingData();
            }
            DoInit();
        }

        public void DoInit()
        {
            if (mSettingData.mQualityTotal != QualityLevel.Custom)
                GameSettingTool.SetQualityLevel(mSettingData.mQualityTotal);
            else
            {
                GameSettingTool.SetMasterTextureLimit(mSettingData.mQualityTexture);
                GameSettingTool.SetShadow(mSettingData.mQualityShadow);
                //GameSettingTool.SetEffect(mSettingData.mQualityEffect);
                GameSettingTool.SetAntiAliasing(mSettingData.mQualityAntialiasing);
            }
        }

        public GameSettingData GetSettingData()
        {
            return mSettingData;
        }

        public void DoSave()
        {
            if (mModify.IsModify)
            {
                //SaveCache(mSettingData);
                mModify.ClearModify();
            }
        }
        #region 音效
        public void SetVolume(VolumeType type, int volume)
        {
            switch (type)
            {
                case VolumeType.Main:
                    if (mSettingData.mVolumeMain != volume)
                    {
                        mSettingData.mVolumeMain = volume;
                        AudioManager.Instance().SetVolumeChange(volume, AudioManager.eAudioType.eAT_All);
                        mModify.MakeModify();
                    }
                    break;
                case VolumeType.Music:
                    if (mSettingData.mVolumeMusic != volume)
                    {
                        mSettingData.mVolumeMusic = volume;
                        AudioManager.Instance().SetVolumeChange(volume, AudioManager.eAudioType.eAT_Backround);
                        mModify.MakeModify();
                    }
                    break;
                case VolumeType.Effect:
                    if (mSettingData.mVolumeEffect != volume)
                    {
                        mSettingData.mVolumeEffect = volume;
                        AudioManager.Instance().SetVolumeChange(volume, AudioManager.eAudioType.eAT_Skill);
                        mModify.MakeModify();
                    }
                    break;
            }
        }
        public int GetVolume(VolumeType type)
        {
            int volume = 0;
            switch (type)
            {
                case VolumeType.Main:
                    volume = mSettingData.mVolumeMain;

                    break;
                case VolumeType.Music:
                    volume = mSettingData.mVolumeMusic;

                    break;
                case VolumeType.Effect:
                    volume = mSettingData.mVolumeEffect;
                    break;
            }
            return volume;
        }
        #endregion

        #region 图像
        /// <summary>
        /// 推荐设置
        /// </summary>
        /// <returns></returns>
        public int GetRecommendQualityLevel()
        {
            return (int)QualityLevel.High;
        }
        /// <summary>
        /// 设置 品质
        /// </summary>
        /// <param name="setType"></param>
        /// <param name="level"></param>
        public void SetQualityLevel(int setType, int level)
        {
            if (level == GetQualityValue(setType))
                return;

            QualityLevel newQuality = (QualityLevel)level;

            switch ((QualitySetType)setType)
            {
                case QualitySetType.Total:
                    mSettingData.mQualityTotal = newQuality;
                    GameSettingTool.SetQualityLevel(newQuality);
                    OnTotalQualitySetChange();
                    break;
                case QualitySetType.Texture:
                    mSettingData.mQualityTexture = newQuality;
                    GameSettingTool.SetMasterTextureLimit(mSettingData.mQualityTexture);
                    OnOtherQualitySetChange();
                    break;
                case QualitySetType.Shadow:
                    mSettingData.mQualityShadow = newQuality;
                    GameSettingTool.SetShadow(mSettingData.mQualityShadow);
                    OnOtherQualitySetChange();
                    break;
                case QualitySetType.Effect:
                    mSettingData.mQualityEffect = newQuality;
                    OnOtherQualitySetChange();
                    break;
                case QualitySetType.Antialiasing:
                    mSettingData.mQualityAntialiasing = newQuality;
                    GameSettingTool.SetAntiAliasing(mSettingData.mQualityAntialiasing);
                    OnOtherQualitySetChange();
                    break;
                case QualitySetType.Bloom:
                    mSettingData.mQualityLight = newQuality;
                    GameSettingTool.SetBloom();
                    OnOtherQualitySetChange();
                    break;
            }
            mModify.MakeModify();
        }

        /// <summary>
        /// 整体质量 设置 发生变化的时候，动态设置 其他选项
        /// </summary>
        void OnTotalQualitySetChange()
        {
            if (mSettingData.mQualityTotal != QualityLevel.Custom)
            {
                mSettingData.mQualityTexture = mSettingData.mQualityTotal;
                mSettingData.mQualityShadow = mSettingData.mQualityTotal;
                mSettingData.mQualityEffect = mSettingData.mQualityTotal;
                mSettingData.mQualityAntialiasing = mSettingData.mQualityTotal;
                if (mSettingData.mQualityTotal == QualityLevel.Low)
                    mSettingData.mQualityLight = QualityLevel.Middle;
                else
                    mSettingData.mQualityLight = QualityLevel.High;
            }
            GameSettingTool.SetMasterTextureLimit(mSettingData.mQualityTexture);
            GameSettingTool.SetShadow(mSettingData.mQualityShadow);
            GameSettingTool.SetAntiAliasing(mSettingData.mQualityAntialiasing);
            GameSettingTool.SetBloom();
        }

        /// <summary>
        /// 子设置 发生变化的时候，动态设置 设置 整体质量为 自定义
        /// </summary>
        void OnOtherQualitySetChange()
        {
            mSettingData.mQualityTotal = QualityLevel.Custom;
        }

        public int GetQualityValue(int setType)
        {
            switch ((QualitySetType)setType)
            {
                case QualitySetType.Total:
                    return (int)mSettingData.mQualityTotal;
                case QualitySetType.Texture:
                    return (int)mSettingData.mQualityTexture;
                case QualitySetType.Shadow:
                    return (int)mSettingData.mQualityShadow;
                case QualitySetType.Effect:
                    return (int)mSettingData.mQualityEffect;
                case QualitySetType.Antialiasing:
                    return (int)mSettingData.mQualityAntialiasing;
                case QualitySetType.Bloom:
                    return (int)mSettingData.mQualityLight;
            }
            return -1;
        }

        public bool CameraPostProcessing()
        {
            return mSettingData.mQualityLight == QualityLevel.High;
        }
        #endregion

        #region 语言
        public void SetLanguageText(int languageType)
        {
            if (mSettingData.mTextLanguage == (ELanguageType)languageType)
                return;
            mSettingData.mTextLanguage = (ELanguageType)languageType;
            mModify.MakeModify();
        }

        public void SetLanguageAudio(int languageType)
        {
            if (mSettingData.mAudioLanguage == (ELanguageType)languageType)
                return;
            mSettingData.mAudioLanguage = (ELanguageType)languageType;
            mModify.MakeModify();
        }

        public int GetLanguageText()
        {
            return (int)mSettingData.mTextLanguage;
        }

        public int GetLanguageAudio()
        {
            return (int)mSettingData.mAudioLanguage;
        }

        #endregion

        #region 多人战斗设置
        public bool GetShowInvite()
        {
            return mSettingData.showInvite == 1;
        }
        public bool GetInFriend()
        {
            return mSettingData.inFriend == 1;
        }
        public bool GetInGuild()
        {
            return mSettingData.inGuild == 1;
        }
        public bool GetInAll()
        {
            return mSettingData.inAll == 1;
        }

        public void SetInvite(bool showInvite, bool inFriend, bool inGuild, bool inAll)
        {
            mSettingData.showInvite = showInvite ? 1 : 0;
            mSettingData.inFriend = inFriend ? 1 : 0;
            mSettingData.inGuild = inGuild ? 1 : 0;
            mSettingData.inAll = inAll ? 1 : 0;
            mModify.MakeModify();
        }
        #endregion
    }
}
